<!--
 * @Date: 2021-06-22 14:12:19
 * @LastEditors: Mingdeng
 * @LastEditTime: 2021-06-23 15:13:00
 * @FilePath: /m-uniapp-template/components/m-map/m-map.vue
-->
<template>
  <view
    class="form-selector m-map-c"
    :class="[{ active: !!value }]"
    @tap="handleChooseAddr"
  >
    {{ value || placeholder }}
    <view class="select-icon m-iconfont">&#xe63e;</view>
  </view>
</template>

<script>
export default {
  name: "m-map",
  props: {
    placeholder: {
      type: String,
      default: "请选择",
    },
    /*address*/
    value: {
      type: String,
      default: "",
    },
  },
  data() {
    return {};
  },
  methods: {
    handleChooseAddr() {
      uni.chooseLocation({
        success: ({ name, address, longitude, latitude }) => {
          this.$emit("input", [name, address, longitude, latitude]);
        },
        fail: ({ errMsg }) => {
          if (errMsg.indexOf("auth deny") !== -1) {
            uni.showModal({
              title: "",
              content: "请在设置中允许小程序使用您的位置",
              cancelColor: "#666666",
              confirmText: "前往设置",
              success: (resToast) => {
                // console.log(resToast);
                if (resToast.confirm) {
                  uni.openSetting({});
                }
              },
            });
          } else {
            uni.showToast({
              title: errMsg,
              icon: "none",
            });
          }
        },
      });
    },
  },
};
</script>

<style lang="scss"></style>
